<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TypesController extends Controller {

    //单图上传
    public function upload($file, $m_size, $allowed_extensions, $filedir) {

        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return "<script>alert('只能上传png，jpg，gif格式');history.go(-1);location.reload(); </script>";
        }
        if ($file->getClientSize() > $m_size) {
            return "<script>alert('主图图片大小不能超过1M');history.go(-1);location.reload(); </script>";
        }
        $fileName = $file->getClientOriginalName(); //3、获取上传图片的文件名  
        $extension = $file->getClientOriginalExtension(); //4、获取上传图片的后缀名  
        $newImagesName = md5(time()) . "." . $extension; //5、重新命名上传文件名字 
        $file->move($filedir, $newImagesName); //6、使用move方法移动文件 
        $path = "/" . $filedir . "/" . $newImagesName; //定义数据库储存的路径
        return $path; //将图片路径插入到表单提交的数组当中
    }

    //多图上传
    public function uploads($subgraph, $m_size, $allowed_extensions, $filedir, $id) {
        foreach ($subgraph as $s) {
            if ($s->getClientSize() > $m_size) {
                return "<script>alert('子图图片大小不能超过1M');history.go(-1);location.reload(); </script>";
            }
            if ($s->getClientOriginalExtension() && !in_array($s->getClientOriginalExtension(), $allowed_extensions)) {
                return "<script>alert('只能上传png，jpg，gif格式');history.go(-1);location.reload(); </script>";
            }
            $s->move($filedir . "/zi", $s->getClientOriginalName());
            $s_path = "/" . $filedir . "/" . "zi/" . $s->getClientOriginalName(); //子图数据库储存的路径 
            if (\DB::table("goodimg")->insert(array('gid' => $id, 'img' => $s_path))) {
                
            } else {
                exit("失败");
            }
        }
    }

    //后台商品显示列表
    public function index() {
        $data = \DB::table('goods')->orderBy('id','asc')->paginate(10);
        return view("types.product_list")->with("data", $data);
    }

    //后台商品修改页面
    public function edit($id) {
        $data = \DB::table('goods')->where("id", $id)->first();
        $cate = \DB::select('select types.*,concat(path,id) p from types order by p');
        $info = \DB::table('goodimg')->where("gid", $id)->get();
        return view("types.product_edit")->with("data", $data)->with("cate", $cate)->with("info", $info);
    }

    //后台添加商品页面 admin/types/create 
    public function create() {
        $data = \DB::select('select types.*,concat(path,id) p from types order by p');
        return view("types.product_add")->with("data", $data);
    }

    //插入操作 post admin/types post 添加商品
    public function store(Request $request) {
        $str = $_POST; //获取表单提交的所有数据  
        unset($str['_token']); //去除token 
        //处理上传的图片
        $file = $request->file('photo'); //获取主图片 
        $m_size = 1048576; //限制上传图片的大小单位字节，现在大小是不得超过1M
        $subgraph = $request->file('subgraph'); //获取子图片 
        $allowed_extensions = ["png", "jpg", "JPG", "gif"];
        $filedir = "uploads/goods/" . date('Y-m-d', time()); //2、定义图片上传路径  
        if(!empty($file)){
            $path = $this->upload($file, $m_size, $allowed_extensions, $filedir);
            $str['img'] = $path;
        }else{
            exit("请上传主图");
        }
        $data = \DB::table("goods")->insert($str);
        $id = \DB::getPdo()->lastInsertId(); //获取刚刚插入产品的id 
        if ($id) {
            if(!empty($subgraph)){
                $this->uploads($subgraph, $m_size, $allowed_extensions, $filedir, $id);
            } 
            return redirect('/admin/types'); //添加成功跳转到商品列表 
        }
    }

    //后台删除商品页面 admin/admin/{admin}
    public function destroy($id) {
        if(\DB::table("goods")->where("id",$id)->delete()){
            return 1;
        }else{
            return "删除失败";
        }
    }

    //后台修改商品页面 admin/types/{type}  put
    public function updates(Request $request) {
        $str = $_POST; //获取表单提交的所有数据 
        $id = $str['id'];
        unset($str['_token']); //去除token 
        unset($str['id']);
        //处理上传的图片
        $file = $request->file('photo'); //获取主图片 
        $m_size = 1048576; //限制上传图片的大小单位字节，现在大小是不得超过1M
        $subgraph = $request->file('subgraph'); //获取子图片 
        $allowed_extensions = ["png", "jpg", "JPG", "gif"];
        $filedir = "uploads/goods/" . date('Y-m-d', time()); //2、定义图片上传路径  
        if ($file) {
            $path = $this->upload($file, $m_size, $allowed_extensions, $filedir);
            $str['img'] = $path; //将图片路径插入到表单提交的数组当中   
        }
        \DB::table("goods")->where("id", $id)->update($str);
        if ($subgraph) {
            \DB::table("goodimg")->where("gid", $id)->delete(); //先删除之前的路径 
            $this->uploads($subgraph, $m_size, $allowed_extensions, $filedir, $id);
        }
        return redirect('/admin/types'); //添加成功跳转到商品列表 
    }

    public function ExitStatus(Request $request) {
        $str=$request->all(); 
        $id=$str['id'];
        unset($str['_token']);
        unset($str['id']);
        unset($str['_method']);
        if(\DB::table("goods")->where("id",$id)->update($str)){
            echo 1;
        }else{
            echo "状态修改失败";
        }
    }
    //商品批量删除
     public function all_del(Request $request){
         $ids=$_POST['ids'];
         if(\DB::delete("delete from goods where id in(".$ids.")")){
             return 1;
         }else{
             return "删除选中栏目失败";
         }
    }
    
    public function res(Request $request){
        $str=$request->all();
        unset($str["_token"]);
        $data=\DB::table("goods")->where("title","like",$str['name']."%")->get();
        return view("types.product_res")->with("data",$data);
    }

// |        | POST      | admin/types                                | types.store            | App\Http\Controllers\admin\TypesController@store      | web
//|        | GET|HEAD  | admin/types                                | types.index            | App\Http\Controllers\admin\TypesController@index      | web
//|        | GET|HEAD  | admin/types/create                         | types.create           | App\Http\Controllers\admin\TypesController@create     | web
//|        | DELETE    | admin/types/{type}                         | types.destroy          | App\Http\Controllers\admin\TypesController@destroy    | web
//|        | PUT|PATCH | admin/types/{type}                         | types.update           | App\Http\Controllers\admin\TypesController@update     | web
//|        | GET|HEAD  | admin/types/{type}                         | types.show             | App\Http\Controllers\admin\TypesController@show       | web
//|        | GET|HEAD  | admin/types/{type}/edit                    | types.edit             | App\Http\Controllers\admin\TypesController@edit       | web
}
